"""
* @描述: 启动程序
* @File: main.py
* ======★★★
* @作者: 星队
* ======★★★
* @创建时间: 2025-05-13 10:33
"""

from flask import Flask,render_template
from models import init_db, Bar, Line, Pie

app = Flask(__name__)

# 初始化数据库
init_db(app)

@app.route('/')
def hello():
    return "Hello, World!"

@app.route('/bar')  # 添加/bar路由
def show_bar():
    # 获取数据库数据
    bars = Bar.query.all()

    bar_data = {
            'names': [bar.name for bar in bars],
            'counts': [bar.counts for bar in bars]
        }
    return render_template('bar.html', chart_data=bar_data)


@app.route('/line')
def show_line():
    # 获取数据库数据
    lines = Line.query.all()

    line_data = {
            'names': [line.name for line in lines],
            'counts': [line.counts for line in lines]
        }
    return render_template('line.html', chart_data=line_data)


@app.route('/pie')
def show_pie():
    sources = Pie.query.all()

    chart_data = {
        "title": "网站访问来源统计",
        "subtitle": "实时数据库数据",
        "legend": [source.name for source in sources],
        "series": [{"value": source.value, "name": source.name} for source in sources]
    }

    return render_template('pie.html', chart_data=chart_data)


if __name__ == '__main__':
    app.run(debug=False)